﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="entity.js.aspx.cs" Inherits="ASB.CC.Web.CCEntityJS" %>

<%--缓存11天--%>
<%--<%@ OutputCache Duration="999999" VaryByParam="none" %>--%>

<%@ Import Namespace="global::Resources" %>

<%--此函数只能在EntityPage上执行，目的是公用一些代码并且全球化文字--%>
$(function(){

    <%--以下定义一些基础方法--%>
    var SetToolTip = function(selector, value){
        $(selector).attr("title", value);
    };

    var SetTextToolTip = function(selector, text, tooltip){
        $(selector).attr("title", tooltip).val(text);
    };
    
    <%--以下是GridView的Action Field的Title--%> 
    SetToolTip(".entity-view", "<%=GlobalButton.ViewTips%>");
    SetToolTip(".entity-modify", "<%=GlobalButton.ModifyTips%>");
    SetToolTip(".entity-disable", "<%=GlobalButton.DeleteTips%>");
    SetToolTip(".entity-download a", "<%=GlobalButton.DownloadTips%>");
    SetToolTip(".entity-download-disabled a", "<%=GlobalButton.DownloadDisabledTips%>");
        
    <%--以下是Pager的Title--%> 
    SetToolTip("a.pager-icon", "<%=GlobalButton.GoToPageTips%>");
    SetToolTip("a.pager-first", "<%=GlobalButton.FirstPageTips%>");
    SetToolTip("a.pager-prev", "<%=GlobalButton.PreviousPageTips%>");
    SetToolTip("a.pager-next", "<%=GlobalButton.NextPageTips%>");
    SetToolTip("a.pager-last", "<%=GlobalButton.LastPageTips%>");
    SetToolTip("span.role-admin", "<%=GlobalButton.AdministratorTips%>");
    
    <%--以下是DetailsView的Action Field的Text和Title--%> 
    SetTextToolTip(".entity-save", "<%=GlobalButton.Save%>", "<%=GlobalButton.SaveTips%>");
    SetTextToolTip(".entity-cancel", "<%=GlobalButton.Cancel%>", "<%=GlobalButton.CancelTips%>");
    SetTextToolTip(".entity-add", "<%=GlobalButton.Create%>", "<%=GlobalButton.CreateTips%>");
    SetTextToolTip(".entity-search", "<%=GlobalButton.Search%>", "<%=GlobalButton.SearchTips%>");
    SetTextToolTip(".entity-clear", "<%=GlobalButton.Clear%>", "<%=GlobalButton.ClearTips%>");
    SetTextToolTip(".entity-close", "<%=GlobalButton.Close%>", "<%=GlobalButton.CloseTips%>");
    SetTextToolTip(".entity-insert-more", "<%=GlobalButton.InsertMore%>", "<%=GlobalButton.InsertMoreTips%>");
    
    <%--以下是Entity页面所有button上样式--%>
    $("input:submit,input:button").addClass("btn");
    
    <%--以下是Entity页面所有delete按钮加confirm--%>
    $(".entity-disable").bind("click", function() {
        return confirm("<%=Messages.ConfirmDelete%>");
    });
    
    <%--以下是Entity页面insert-more按钮加事件--%>
    $(".entity-insert-more").bind("click", function () {
        $(this).parent().append("<input type='hidden' name='insert$more' />");
        $(this).prev().prev().click();
        return false;
    });
    <%--以下是Entity页面cancel, save按钮加事件--%>
<%--    $(".entity-cancel,.entity-save").each(function(){
        var fn = $(this).attr("onclick");
        $(this).attr("onclick", "").bind("click", function () {
            $("input[name='insert$more']").remove();
        }).bind("click", fn);
    });--%> 
   
    <%--奇数行偶数行背景色--%>
    $(".tablestyle tr:even").addClass("trwhitebg");
    $(".tablestyle tr:odd").addClass("trpurplebg");

    <%--列表鼠标效果--%>
    $('.tbody').
        mouseover(function() {
            $(this).addClass('trhover');
        }).
        mouseout(function() {
            $(this).removeClass('trhover');
        });
        
    <%--搜索框效果--%>
    $('.entity-search-text').
        addClass('schgrey').
        mouseover(function() {
            $(this).addClass('txtboxover').removeClass('schgrey');
            if($(this).val() == $(this).attr("tips"))
                $(this).val("");
        }).
        mouseout(function() {
            if(document.activeElement.className.indexOf('entity-search-text') < 0)
            {
                $(this).removeClass('txtboxover').addClass('schgrey');
                if($(this).val() == '')
                    $(this).val($(this).attr("tips"));
            }

        }).
        bind("keydown", function(e) {
            if (e.keyCode == '13') {
                e.preventDefault();
                $(".entity-search").click();
            }
        }).each(function(){
            if($(this).attr("tips") == null)
                $(this).attr("tips", "<%=Messages.SearchTips %>");
            if($(this).val() == '')
                $(this).val($(this).attr("tips"));
            $(this).attr("title", "<%=Messages.SearchKeywordFormat %>" + $(this).attr("title"));
        });

    <%--分页框框效果--%>
    $(".pager-text").
        mouseover(function() {
            $(this).addClass('txtboxover');
        }).
        mouseout(function() {
            $(this).removeClass('txtboxover');
        });<%--.
        bind("keydown", function(e) {
            if (e.keyCode == '13') {
                e.preventDefault();
                //$(this).next().click();
                $("a.pager-icon").click();
            }
            else if ((e.keyCode < '48' /*0*/ || e.keyCode > '57' /*9*/) && e.keyCode != '8' /*backspace*/)
                return false;
        });--%>
        
        
    <%-- ccs 原page控件tips添加 --%>
    var td = $("table.pagecounting td:eq(1)");
    td.children("input:eq(0):enabled").attr("title","<%=GlobalButton.FirstPageTips%>");
    td.children("input:eq(1):enabled").attr("title","<%=GlobalButton.PreviousPageTips%>");
    td.children("input:eq(2):enabled").attr("title","<%=GlobalButton.NextPageTips%>");
    td.children("input:eq(3):enabled").attr("title","<%=GlobalButton.LastPageTips%>");
    td.children("input:eq(5):enabled").attr("title","<%=GlobalButton.GoToPageTips%>");
    
    $(".entity-footer").appendTo("div.formbox");
    
});



function InputUIShow()
{
    $(".entity-notnull-check").each(function(index) {
        if($(this).find("input,textarea").length != 0)
            if($(this).prev().text().indexOf('*') == -1)
                $(this).prev().append("<font style='color: red'>*&nbsp;</font>");
    });
    $(".entity-selects-check").each(function(index) {
        if($(this).find("select").length != 0)
            if($(this).prev().text().indexOf('*') == -1)
                $(this).prev().append("<font style='color: red'>*&nbsp;</font>");
    });

    $(".entity-selects-nullable-check").each(function(index) {
        
            
    });
    $(".entity-checkboxes-check").each(function(index) {
        if($(this).find("input").length != 0)
            if($(this).prev().text().indexOf('*') == -1)
                $(this).prev().append("<font style='color: red'>*&nbsp;</font>");
    });
    $(".entity-radios-check").each(function(index) {
        if($(this).find("input").length != 0)
            if($(this).prev().text().indexOf('*') == -1)
                $(this).prev().append("<font style='color: red'>*&nbsp;</font>");
    });
}

function InputCheck() {
    var result = "";
    <%--for entity-notnull-check--%>
    $(".entity-notnull-check").each(function(index) {
        var inputfield = $(this).children().first();
        if (inputfield.val() == "") {
            result += $(this).prev().text() + " could not be empty!\n";
            inputfield.addClass("validation-error");
        }
        else {
            inputfield.removeClass("validation-error");
        }
    });
    
    <%--for entity-datetime-check--%>
    var regtime = new RegExp("^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$");
    $(".entity-datetime-check").each(function(index) {
        var datetimefield = $(this).children().first();
        var valuedatetime = datetimefield.val();
        if (valuedatetime != "" && !regtime.test(valuedatetime)) {
            result += $(this).prev().text() + " has invalid date format!\n";
            datetimefield.addClass("validation-error");
        }
        else {
            datetimefield.removeClass("validation-error");
        }
    });
    <%--for entity-email-check--%>
    var regemail = new RegExp("^[0-9a-zA-Z_\.]+@[0-9a-zA-Z_]+[\.]{1}[0-9a-zA-Z_]+[\.]?[0-9a-zA-Z_]+$");
    $(".entity-email-check").each(function(index) {
        var emailfield = $(this).children().first();
        var valueemail = emailfield.val();
        if (valueemail != "" && !regemail.test(valueemail)) {
            result += $(this).prev().text() + " has invalid Email format!\n";
            emailfield.addClass("validation-error");
        }
        else {
            emailfield.removeClass("validation-error");
        }
    });

    <%--for entity-selects-check--%>
    $(".entity-selects-check").each(function(index) {
        var selectfield = $(this).children().first();
        if (selectfield.val() == "0" || selectfield.val() == "" || selectfield.val() == "--请选择--" || selectfield.val() == "-1") {
            result += $(this).prev().text() + " could not be empty!\n";
            selectfield.addClass("validation-error");
        }
        else {
            selectfield.removeClass("validation-error");
        }
    });
    
    <%--for entity-selects2-check--%>
    $(".entity-selects2-check").each(function(index) {
        var selectfield1 = $(this).children().first();
        var selectfield2 = $(this).children().first().next();
        if (selectfield1.val() == "0" || selectfield2.val() == "0" ) {
            result += "P\CAT could not be empty!\n";
            selectfield1.addClass("validation-error");
            selectfield2.addClass("validation-error");
        }
        else {
            selectfield1.removeClass("validation-error");
            selectfield2.removeClass("validation-error");
            
        }
    });
    
    <%--for entity-selects3-check--%>
    $(".entity-selects3-check").each(function(index) {
        var selectfield1 = $(this).children().first();
        var selectfield2 = $(this).children().first().next();
        if (selectfield1.val() == "0" && selectfield2.val() == "0" ) {
            result += "P\CAT could not be empty!\n";
            selectfield1.addClass("validation-error");
            selectfield2.addClass("validation-error");
        }
        else {
            selectfield1.removeClass("validation-error");
            selectfield2.removeClass("validation-error");
            
        }
    });
    

    
    <%--for entity-number-check--%>
    $(".entity-number-check").each(function(index) {
        var selectfield = $(this).children().first();
       
        if (!/^[0-9_-]+$/.test(selectfield.val())) {
            result += $(this).prev().text() + " has invalid type!\n";
            selectfield.addClass("validation-error");
        }
        else {
            selectfield.removeClass("validation-error");
        }
    });
    
    <%--for entity-selects-nullable-check--%>
    $(".entity-selects-nullable-check").each(function(index) {
        var selectfield = $(this).children().first();
        if (selectfield.val() == "-1") {
            result += $(this).prev().text() + " Wrong Data!\n";
            selectfield.addClass("validation-error");
        }
        else {
            selectfield.removeClass("validation-error");
        }
    });
    <%--for entity-radios-check--%>
    $(".entity-radios-check").each(function(index) {
        var radiosfield = $(this);
        if (radiosfield.find("input:checked").length == 0) {
            result += $(this).prev().text() + " could not be empty!\n";
            radiosfield.addClass("validation-error");
        }
        else {
            radiosfield.removeClass("validation-error");
        }
    });
    <%--for entity-checkboxes-check--%>
    $(".entity-checkboxes-check").each(function(index) {
        var checkboxesfield = $(this);
        if (checkboxesfield.find("input:checked").length == 0) {
            result += $(this).prev().text() + " could not be empty!\n";
            checkboxesfield.addClass("validation-error");
        }
        else {
            checkboxesfield.removeClass("validation-error");
        }
    });
    
    
    <%--for entity-length-check--%>
    $(".entity-length-check").each(function(index) {
        var inputfield = $(this).children().first();
        var lengthRequired;
        try
        {
            lengthRequired = parseInt($(this).attr('maxLength'));
        }
        catch(exp)
        {
            lengthRequired = 9;
        }
        if (inputfield.val().length > lengthRequired) {
            result += $(this).prev().text() + " must not longer than " + lengthRequired + " characters";
            inputfield.addClass("validation-error");
        }
        else {
            inputfield.removeClass("validation-error");
        }
    });
    
    if (result != "") {
        alert(result);
        event.returnValue = false;
        return false;
    }
    else {
        return true;
    }
}

$(function() {
    <%--UI show--%>
    InputUIShow();
    
    <%--submit event--%>
    $(".entity-save").each(function(index) {
        $(this).bind("click", function() {
            var flag = InputCheck();
            
            if(!flag){
                $("input[name='insert$more']").remove();
            }
             <%--待删除--%>
            if('redirect' in window) redirect(flag);
            
            return flag;
        });
    });

    
    <%--窗口unload事件--%>
    $(window).bind("beforeunload", function(){
      <%--  $("select").hide();--%>
        $("#dialog-message").dialog({
		    modal: true
        });
    });

    <%-- //remove CR -B --%>
    $(".hand_span").each(function() {
        var appNo = $(this).text();
        <%-- getCRNonB is in common.js --%>
        $(this).text(getCRNonB($.trim(appNo)));
    });

});

